package com.chw.admin.permission;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.chw.annotation.PermissionSecurity;
import com.chw.common.ServiceData;
import com.chw.constants.Constants;
import com.chw.utils.CommonFieldsUtil;
import com.chw.utils.LogUtil;

@Controller
@RequestMapping("/sysPermission")
public class SysPermissionController {
	
	private static LogUtil log = LogUtil.newInstance(SysPermissionController.class);

	@Autowired
	private ISysPermissionService sysPermissionService;

	@RequestMapping(value = "/pageModule", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name = "查询", module = "模块管理", btnfunc = "select_module")
	public ServiceData pageModule(HttpServletRequest request,String modulename, Integer currentPage, Integer pageSize)
			throws Exception {
		log.info("pageModule");
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		PageInfo<Map<String,Object>> permissionModule = sysPermissionService.pageModule(modulename, currentPage, pageSize);
	    ret.setCode(request, ServiceData.RetCode.Success);
	    ret.setBo(permissionModule);
		return ret;
	}
	
	@RequestMapping(value = "/deleteModule", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public void deleteModule(HttpServletRequest request,ModulePermission record)
			throws Exception {
		log.info("deleteModule");
		sysPermissionService.deleteModule(record);
	}
	
	/**
	 * 通过用户id、用户组id、角色id获取对应菜单权限
	 * @param request
	 * @param id
	 * @param type
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getMenuPermission", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name = "查询", module = "菜单权限管理", btnfunc = "")
	public ServiceData getMenuPermission(HttpServletRequest request,String id,String type) throws Exception{
		log.info("getMenuPermission");
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		List<Map<String,Object>> list = sysPermissionService.getMenuPermission(id,type);
	    ret.setCode(request, ServiceData.RetCode.Success);
	    ret.setBo(list);
		return ret;
	}
	
	/**
	 * 通过用户id、用户组id、角色id获取对应功能模块权限
	 * @param request
	 * @param id
	 * @param type
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getFuncPermission", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name = "查询", module = "功能权限管理", btnfunc = "")
	public ServiceData getFuncPermission(HttpServletRequest request,String id,String type) throws Exception{
		log.info("getFuncPermission");
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		List<Map<String,Object>> list = sysPermissionService.getFuncPermission(id,type);
	    ret.setCode(request, ServiceData.RetCode.Success);
	    ret.setBo(list);
		return ret;
	}

	/**
	 * 通过用户id、用户组id、角色id新增或更新对应权限状态
	 * @param request
	 * @param pkid
	 * @param id
	 * @param type
	 * @param permissionid
	 * @param flag
	 * @throws Exception
	 */
	@RequestMapping(value = "/saveOrUpdatePermission", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name = "分配权限", module = "权限管理", btnfunc = "")
	public void saveOrUpdatePermission(HttpServletRequest request,String id,String type,String permissionid,String flag) throws Exception{
		log.info("saveOrUpdatePermission");
		sysPermissionService.saveOrUpdatePermission(id,type,permissionid,flag);
	}
	
	@RequestMapping(value = "/findModulePermission.notPermSc", method = RequestMethod.GET)
	public void findModulePermission(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String userId = CommonFieldsUtil.getCurrentUserId();
		PrintWriter pw = response.getWriter();
		if(StringUtils.isNotBlank(userId) && Constants.ADMIN_ID.equals(userId))
		{
			pw.write("<style></style>");
		}else{
			List<Map<String,Object>> list = sysPermissionService.findModulePermissionByUser();
			StringBuffer sb = new StringBuffer();
			for(Map<String,Object> map : list){
				sb.append(":not(.").append(map.get("btn_func")).append(")");
			}
			pw.write("<style>.funcbtn"+sb.toString()+"{display:none}</style>");
		}
		pw.flush();
		pw.close();
	}
}
